home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 441 / dlibs12 / fflush.c < prev    next >
C/C++ Source or Header  |  1990-11-23  |  1KB  |  63 lines

  1. #include <stdio.h>
  2.  
  3. extern    FILE    _iob[];
  4.  
  5. int fflush(fp)
  6.     register FILE *fp;
  7. /*
  8.  *    implementation note:  This function has the side effect of
  9.  *    re-aligning the virtual file pointer (in the buffer) with
  10.  *    the actual file pointer (in the file) and is therefore used
  11.  *    in other functions to accomplish this re-sync operation.
  12.  */
  13.     {
  14.     register int f, i;
  15.  
  16.      if(fp)
  17.         return(_fflush(fp));
  18.     else
  19.         {
  20.         for(i=0; i<_NFILE; ++i)
  21.             {
  22.             f = _iob[i]._flag;
  23.             if(f & (_IOREAD | _IOWRT))
  24.                 _fflush(&_iob[i]);
  25.             }
  26.         return(0);
  27.         }
  28.     }
  29.  
  30. static int _fflush(fp)
  31.     register FILE *fp;
  32.     {
  33.     register int f, rv = 0, c;
  34.     register long offset;
  35.  
  36.      if(fp == NULL)
  37.         return(0);
  38.     f = fp->_flag;
  39.     if((f & _IODEV)                /* file is a device */
  40.     || (!(f & (_IOREAD | _IOWRT))))        /* file not open! */
  41.         return(0);
  42.     if(fp->_cnt)                /* data in the buffer */
  43.         {
  44.         if(f & _IORW)                /* writing */
  45.             {
  46.             if(write(fp->_file, fp->_base, fp->_cnt) != fp->_cnt)
  47.                 {
  48.                 fp->_flag |= _IOERR;
  49.                 rv = EOF;
  50.                 }
  51.             }
  52.         else                    /* reading */
  53.             {
  54.             offset = -(fp->_cnt);
  55.             if(lseek(fp->_file, offset, 1) < 0)
  56.                 rv = EOF;
  57.             }
  58.         }
  59.     fp->_ptr = fp->_base;
  60.     fp->_cnt = 0;
  61.     return(rv);
  62.     }
  63.